<script setup lang="ts">
import { ref } from "vue";
import { useOrganization } from "./hook";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import Delete from "@iconify-icons/ep/delete";
import EditPen from "@iconify-icons/ep/edit-pen";
import Search from "@iconify-icons/ep/search";
import Refresh from "@iconify-icons/ep/refresh";
import AddFill from "@iconify-icons/ri/add-circle-line";

defineOptions({
  name: "Organization"
});
const addDtoRef = ref<any>();
const formRef = ref();
const {
  form,
  loading,
  columns,
  dataList,
  pagination,
  loadingTitle,
  updatePanelVisible,
  rules,
  onSearch,
  resetForm,
  handleUpdate,
  handleDelete,
  handleSizeChange,
  ClosePanelVisible,
  handleCurrentChange,
  handleSelectionChange,
  addOrganization
} = useOrganization();
</script>

<template>
  <div class="main">
    <el-form ref="formRef" :inline="true" :model="form" class="bg-bg_color w-[99/100] pl-8 pt-4">
      <el-form-item label="机构名称" prop="OrganizationName">
        <el-input v-model="form.OrganizationName" placeholder="请输入机构名称" clearable class="!w-[180px]" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :icon="useRenderIcon(Search)" :loading="loading" @click="onSearch">
          搜索
        </el-button>
        <!-- <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
          重置
        </el-button> -->
      </el-form-item>
    </el-form>

    <el-dialog @close="ClosePanelVisible(addDtoRef)" v-model="updatePanelVisible" :title="loadingTitle + '机构'" width="30%"
      center :close-on-click-modal="false">
      <el-form ref="addDtoRef" label-position="left" label-width="100px" :rules="rules" :model="form"
        style="max-width: 460px">
        <el-form-item label="机构名称" prop="OrganizationName">
          <el-input v-model="form.OrganizationName" class="w-220" />
        </el-form-item>
        <el-form-item label="用户数量" prop="UserCount">
          <el-input-number type="number" v-model="form.UserCount" :min="1" />
        </el-form-item>
        <el-form-item label="到期时间" prop="ExpirationTime">
          <el-date-picker v-model="form.ExpirationTime" type="datetime" placeholder="请选择到期时间" class="w-220" />
        </el-form-item>
        <el-form-item label="备注" prop="Remarks">
          <el-input type="textarea" v-model="form.Remarks" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="addOrganization(addDtoRef)">
            保存
          </el-button>
          <el-button @click="ClosePanelVisible(addDtoRef)">取消</el-button>
        </span>
      </template>
    </el-dialog>

    <PureTableBar title="机构列表" @refresh="onSearch" class="h100-60">
      <template #buttons>
        <p class="text-red-500 absolute left-[120px] leading-7 text-sm">注：双击查看详情</p>
        <el-button type="primary" :icon="useRenderIcon(AddFill)" @click="handleUpdate(undefined)">
          添加机构
        </el-button>
      </template>
      <template v-slot="{ size, checkList }">
        <pure-table border align-whole="center" showOverflowTooltip table-layout="auto" :loading="loading" :size="size"
          :data="dataList" :columns="columns" :checkList="checkList" :pagination="pagination"
          :paginationSmall="size === 'small' ? true : false" @cell-dblclick="handleUpdate" :header-cell-style="{
            background: 'var(--el-table-row-hover-bg-color)',
            color: 'var(--el-text-color-primary)'
          }" @selection-change="handleSelectionChange" @size-change="handleSizeChange"
          @current-change="handleCurrentChange">
          <template #operation="{ row }">
            <el-button class="reset-margin" link type="primary" :size="size" :icon="useRenderIcon(EditPen)"
              @click="handleUpdate(row)">
              修改
            </el-button>
            <!-- <el-popconfirm title="是否确认删除?" @confirm="handleDelete(row)">
              <template #reference>
                <el-button class="reset-margin" link type="danger" :size="size" :icon="useRenderIcon(Delete)">
                  删除
                </el-button>
              </template>
            </el-popconfirm> -->
            <el-button class="reset-margin" @click="handleDelete(row)" link type="danger" :size="size"
              :icon="useRenderIcon(Delete)">
              删除
            </el-button>
          </template>
        </pure-table>
      </template>
    </PureTableBar>
  </div>
</template>

<style scoped lang="scss">
:deep(.el-dropdown-menu__item i) {
  margin: 0;
}
</style>
